/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.example.controller;

import com.example.model.IMessageResp;
import com.example.model.IsvProduceReq;
import com.example.service.LicenseProduceService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import com.example.util.IsvProduceAPI;
import com.example.util.ResultCodeEnum;

/**
 * License授权码类商品接入示例工程
 */
@RestController
@RequestMapping(value = "isv/produce")
@Slf4j
public class IsvProduceController {

    /**
     * 云市场分配的秘钥，在服务商入驻云市场成功后，isvConsole可以查看,因为安全性，并不建议写死
     */
    @Value("${access_key}")
    private String ACCESS_KEY;

    @Resource
    private LicenseProduceService licenseProduceService;


    /**
     * 授权码生产接口请求处理
     *
     * @param isvProduceReq 请求体
     * @param request       请求
     * @return response     返回
     */
    @PostMapping(value = "")
    @ResponseBody
    public IMessageResp processProduceReq(@RequestBody IsvProduceReq isvProduceReq, HttpServletRequest request) throws Exception {
        // 验证签名
        IMessageResp resp = IsvProduceAPI.verifySignature(isvProduceReq, request, ACCESS_KEY);

        // 如果鉴权返回成功则进行业务处理 区分三种接口 验证参数 及异常场景校验
        if (ResultCodeEnum.SUCCESS.getResultCode().equals(resp.getResultCode())) {
            resp = licenseProduceService.processProduceReq(isvProduceReq);
        }
        return resp;
    }
}

